﻿using System;
using System.Collections.Generic;

class SieveOfEratosthenes
{
    public static void Main(string[] args)
    {
        int lowerBound = 2;
        int upperBound = 100;
        List<int> primeNumbers = GetPrimeNumbers(lowerBound, upperBound);

        Console.WriteLine($"2到100之间的素数有：");
        foreach (int prime in primeNumbers)
        {
            Console.Write(prime + " ");
        }
        Console.Read();
    }

    public static List<int> GetPrimeNumbers(int lowerBound, int upperBound)
    {
        bool[] isPrime = new bool[upperBound + 1];
        for (int i = 2; i <= upperBound; i++)
        {
            isPrime[i] = true;
        }

        for (int i = 2; i * i <= upperBound; i++)
        {
            if (isPrime[i])
            {
                for (int j = i * i; j <= upperBound; j += i)
                {
                    isPrime[j] = false;
                }
            }
        }

        List<int> primeNumbers = new List<int>();
        for (int i = lowerBound; i <= upperBound; i++)
        {
            if (isPrime[i])
            {
                primeNumbers.Add(i);
            }
        }

        return primeNumbers;
    }
}
